package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("TasteDAO")
@Transactional
public class TasteDAOImpl extends AbstractJpaDao<Taste> implements TasteDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Taste.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public TasteDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Taste findTasteById(Integer id) throws DataAccessException {

		return findTasteById(id, -1, -1);
	}

	@Transactional
	public Taste findTasteById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findTasteById", startResult, maxRows, id);
			return (Taste) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Taste> findTasteByName(String name) throws DataAccessException {

		return findTasteByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Taste> findTasteByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTasteByName", startResult, maxRows, name);
		return new LinkedHashSet<Taste>(query.getResultList());
	}

	@Transactional
	public Set<Taste> findTasteByNameContaining(String name) throws DataAccessException {

		return findTasteByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Taste> findTasteByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTasteByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Taste>(query.getResultList());
	}

	@Transactional
	public Set<Taste> findTasteByDescription(String description) throws DataAccessException {

		return findTasteByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Taste> findTasteByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTasteByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Taste>(query.getResultList());
	}

	@Transactional
	public Set<Taste> findTasteByDescriptionContaining(String description) throws DataAccessException {

		return findTasteByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Taste> findTasteByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findTasteByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Taste>(query.getResultList());
	}

	@Transactional
	public Taste findTasteByPrimaryKey(Integer id) throws DataAccessException {
		return findTasteByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Taste findTasteByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findTasteByPrimaryKey", startResult, maxRows, id);
			return (Taste) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Taste> findTastesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findTastesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Taste> findAllTastes() throws DataAccessException {

		return findAllTastes(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Taste> findAllTastes(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllTastes", startResult, maxRows);
		return new LinkedHashSet<Taste>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Taste> findTasteByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Taste>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Taste> findTastesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Taste.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Taste> tastes = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return tastes;
	}

	@Transactional
	public Taste findTasteByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findTasteByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Taste findTasteByPhysicalPrimaryKey(String name, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findTasteByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Taste) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Taste entity) {
		return true;
	}
}
